<script setup>

</script>

<template>
  <el-card>

    <!--查询内容-->
    <el-form ref="queryParams" :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="创建时间" prop="createTime" >
        <el-date-picker
            v-model="queryParams.data.dateRange"
            size="small"
            style="width:240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholde="开始日期"
            end-placeholde="结束日期"
        />
      </el-form-item>
      <el-form-item label="新闻来源">
        <el-select v-model="queryParams.data.newsSource" placeholder="新闻来源" clearable>
          <el-option label="AAA教育" value="AAA教育"></el-option>
          <el-option label="光明日报" value="光明日报"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery" size="small">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="resetQuery()" size="small">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-plus" size="small" @click="inserNews">添加新闻</el-button>
      </el-col>
    </el-row>

    <!--表格内容-->
    <el-table :data="newsList" border height="500px">

      <el-table-column prop="newsId" label="新闻ID"></el-table-column>
      <el-table-column prop="newsTitle" label="新闻标题"></el-table-column>
      <el-table-column prop="newsSource" label="新闻来源"></el-table-column>
      <el-table-column label="新闻图片">
        <template slot-scope="scope">
          <img :src="scope.row.imagUrl" style="height: 100px">
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="180">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="warning" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--分页内容-->

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNo"
        :page-sizes="[5,10,15,20]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!--    添加或修改新闻-->
    <el-dialog
        :title="title"
        :visible.sync="open">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻标题" prop="newsTitle">
          <el-input v-model="form.newsTitle"></el-input>
        </el-form-item>
        <el-form-item label="新闻来源" prop="newsSource">
          <el-select v-model="form.newsSource" placeholder="新闻来源">
            <el-option label="AAA教育" value="AAA教育"></el-option>
            <el-option label="光明日报" value="光明日报"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻内容" prop="newsContent">
          <vue-editor v-model="form.newsContent"></vue-editor>
        </el-form-item>
        <el-form-item label="新闻图片" prop="imagUrl">
          <el-upload
              class="avatar-uploader"
              :headers="headers"
              :action="action"
              :name="file"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="form.imagUrl" :src="form.imagUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="addNews">确 定</el-button>
  </span>
    </el-dialog>
  </el-card>

</template>
<script>
import {VueEditor} from "vue2-editor";

export default {
  //以组件方式使用VueEditor
  components: {
    VueEditor
  },
  data() {
    return {
      queryParams: {
        data:{},
        pageNo: 1,
        pageSize: 5,
      },
      // 图片上传的请求头
      headers: {
        token: sessionStorage.getItem("token")
      },
      action: this.$http.defaults.baseURL + "/fileUpload/upload",
          /*"http://172.16.7.71:88/system/system/news/uploadOos",*/
      total: 0,
      newsList: [],
      open: false,
      title: '',
      form: {
        newsTitle: '',
        newsSource: '',
        newsContent: '',
        imagUrl: ''
      },
      rules: {
        newsTitle: [
          {required: true, message: '请输入新闻标题', trigger: 'blur'}
        ],
        newsSource: [
          {required: true, message: '请输入新闻来源', trigger: 'blur'}
        ],
        newsContent: [
          {required: true, message: '请输入新闻内容', trigger: 'blur'}
        ],
        imagUrl: [
          {required: true, message: '请上传新闻图片', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.formdata()
  },
  methods: {
    //表格数据
    formdata() {
      this.$http.post('/newsA/queryByPage', this.queryParams).then(resp => {
        this.newsList = resp.data.data.list
        this.total = resp.data.data.total
      })
    },
    //搜索
    handleQuery() {
      this.formdata()
    },
    //重置搜索
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 5,
        data:{},
      }
      this.formdata()
      // location.reload()
    },
    //分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.formdata()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNo = val
      this.formdata()
    },
    //添加新闻
    inserNews() {
      this.open = true
      this.title = '添加新闻'
      this.form = {
        newsTitle: '',
        newsSource: '',
        newsContent: '',
        imagUrl: ''
      }
    },
    //重置添加和修改
    resetForm() {

    },
    //删除
    handleDelete(row) {
      this.$confirm('是否删除该新闻?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/newsA/deleteById?newsId=' + row.newsId).then(resp => {
          if (resp.data.code === 200) {
            this.$message.success('删除成功')
            this.formdata()
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    //编辑
    handleUpdate(row) {
      this.open = true
      this.form = row
      this.title = '修改新闻'
    },
    //添加或修改新闻的提交
    addNews() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$http.post("/newsA/insertAndUpdate", this.form).then(resp => {
            // console.log(resp.data)
            if (resp.data.code === 200) {
              this.$message.success('操作成功')
              this.open = false
              this.formdata()
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },
    //添加或修改新闻的取消
    cancel() {
      this.open = false
      this.form = {
        newsTitle: '',
        newsSource: '',
        newsContent: '',
        imagUrl: ''
      }
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    //上传图片
    handleAvatarSuccess(res, file) {
      this.form.imagUrl = res.data
    },
    //上传图片
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>